module.exports = function($scope, $state, orderbackmylist, orderback, $resource,
    $modal, categorylist){

  var order=[
      {
          'label':'平台',
          'value':'juyou',
      },
      {
          'label':'商客',
          'value':'shangke',
      },
      {
          'label':'不限',
          'value':'',
      }
  ];
  $scope.orderarr=order;

	$scope.searchform = {
         'from_app_id' : 'juyou'
    };
    $scope.payment_typearr = [];
    $scope.back_statearr = [];
    $scope.ticket_statearr = [];

    //加载自动退款状态
	$scope.loadbackstate = function (){
		$resource('/api/ac/tc/ticketOrderBackService/saveorgetStateInfo', {}, {}).save({}, function(res){
			if(res.errcode === 0){
                $scope.backState = res.data;
                if($scope.backState.back_state == 1){
                    $scope.backState.back_state = true;
                }else{
                    $scope.backState.back_state = false;
                }
			} else {
				toaster.error({title: '', body: '加载个人信息失败'});
			}
		});
	}
    $scope.loadbackstate();
        //保存自动退款状态
        $scope.updateState = function (info){
            if(info){
                var state = {
                    'back_state' : '1'
                }
            }else{
                var state = {
                    'back_state' : '0'
                }
            }
            
            $resource('/api/as/tc/ticketorderback/updateState', {}, {}).save(state, function(res){
                if(res.errcode === 0){
                    $scope.loadbackstate();
                } else {
                    $scope.backState.back_state = !$scope.backState.back_state;
                    toaster.error({title: '', body: '设置失败！'});
                }
            });
        }
    $scope.rec = function (){
        var mymessage=confirm("当开启自动退款时，将自动退掉所有订单，无需再手动操作。是否继续？");  
        if(mymessage==true)  
        {  
            $scope.updateState($scope.backState.back_state);
        }  
        else if(mymessage==false)  
        {  
            $scope.backState.back_state = !$scope.backState.back_state;
            return;
        }  
    }  

       

	/* 分页   
     * ========================================= */
    $scope.maxSize = 10;            //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10;         //每页显示几条

    $scope.change = function(){
        $scope.bigCurrentPage = 1; 
    }

    $scope.load = function () {

    	var para = {
            pageNo:$scope.bigCurrentPage, 
            pageSize:$scope.itemsPerPage
        };

        para = angular.extend(para, $scope.searchform);
        
        orderbackmylist.save(para, function(res){
         	if(res.errcode !== 0){
         		alert("数据获取失败");
         		return;
         	}
         	$scope.objs = res.data.results;
            $scope.bigTotalItems = res.data.totalRecord;
        });
    };
    $scope.load();

	$scope.back = function(obj){
    var orderback1 = orderback;
		var modalInstance = $modal.open({
	          template: require('../views/orderbackorder.html'),
	          controller: 'orderbackorder',
	          size: 'xs',
	          resolve: {
	            id : function(){
	                return obj.id;
	            },
	            back_price : function(){
	                return obj.back_price;
	            },
	            'orderback' : function(){
	                return orderback1;
	            }
	          }
        });

        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
            
        });
	}


    categorylist.save({'type' : 'ticket_payment_type'},function(res) {
        if(res.errcode === 0){
            $scope.payment_typearr = res.data;
            $scope.payment_typearr.unshift({'label':'不限','value':''});
        } else {
            alert(res.errmsg);
        }
    });

    categorylist.save({'type' : 'ticket_back_pay_state'},function(res) {
        if(res.errcode === 0){
            $scope.back_statearr = res.data;
            $scope.back_statearr.unshift({'label':'不限','value':''});
        } else {
            alert(res.errmsg);
        }
    });

    categorylist.save({'type' : 'ticket_back_ticket_state'},function(res) {
        if(res.errcode === 0){
              $scope.ticket_statearr = res.data;
              $scope.ticket_statearr.unshift({'label':'不限','value':''});
        } else {
            alert(res.errmsg);
        }
    });

};